package com.wwtech.epp.ui.activity.friend.presenter;

import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.entity.GroupMemberme;
import com.wwtech.epp.entity.NodeInfo;
import com.wwtech.epp.entity.NodeMember;
import com.wwtech.epp.ui.activity.friend.FriendListActivity;
import com.wwtech.epp.ui.activity.friend.contract.FriendListContract;

import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.friend
 * @Description: presenter of FriendListActivity
 * @date 2017/09/25 15:56:45
 */
public class FriendListPresenter implements FriendListContract.FriendListContractPresenter{

    HttpAPIWrapper httpAPIWrapper;
    private final FriendListContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private FriendListActivity mActivity;

    @Inject
    public FriendListPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull FriendListContract.View view, FriendListActivity activity) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mActivity = activity;
    }
    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
             mCompositeDisposable.dispose();
        }
    }
    @Override
    public void getNodeMember(String nodeId, Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.getNodeInfo(nodeId, map)
                .subscribe(new Consumer<NodeInfo>() {
                    @Override
                    public void accept(NodeInfo nodeMember) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        mView.groupMemberBack(nodeMember.getData().getList());
                        mView.setTotal(nodeMember.getData().getTotal());
//                        handlerGroupPerson(nodeMember.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }
    @Override
    public void getGroupMember(String groupId, Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.getGroupMember(groupId, map)
                .subscribe(new Consumer<GroupMemberme>() {
                    @Override
                    public void accept(GroupMemberme nodeMember) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        mView.groupMemberBack(nodeMember.getData());
//                        handlerGroupPerson(nodeMember.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                    }
                });
        mCompositeDisposable.add(disposable);
    }
}